using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace TacticalGridWars
{
    /// <summary>
    /// Holds a Grid object and draws it
    /// </summary>
    public class GameGrid : DrawableGameComponent
    {
        #region MemberData
        private SpriteBatch spriteBatch;
        private Texture2D tileTexture;
        private Texture2D coverTexture;
        private Texture2D wallTexture;
        private Texture2D reachbleTexture; 
        #endregion

        #region Properties
        public Grid Grid { get; set; } 
        #endregion

        public GameGrid(Game game)
            : base(game)
        {
            this.spriteBatch = new SpriteBatch(game.GraphicsDevice);
            Grid = new Grid();
        }

        protected override void LoadContent()
        {
            tileTexture = Game.Content.Load<Texture2D>("tile");
            coverTexture = Game.Content.Load<Texture2D>("tile_cover");
            wallTexture = Game.Content.Load<Texture2D>("tile_wall");
            reachbleTexture = Game.Content.Load<Texture2D>("reachable");
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            for (int i = 0; i < Grid.Columns; i++)
            {
                for (int j = 0; j < Grid.Rows; j++)
                {
                    if (Grid.Level[j,i].State == Grid.TileState.Cover)
                    {
                        spriteBatch.Draw(coverTexture, new Vector2(i * 50, j * 50), Color.Cyan * 0.5f);
                    }
                    else if (Grid.Level[j,i].State == Grid.TileState.Wall)
                    {
                        spriteBatch.Draw(wallTexture, new Vector2(i * 50, j * 50), Color.Blue * 0.75f);
                    }
                    else
                    {
                        if (Grid.Level[j,i].WalkReachable)
                        {
                            spriteBatch.Draw(reachbleTexture, new Vector2(i * 50 + 10, j * 50 + 10), Color.Yellow * 0.1f);
                        }
                        else if (Grid.Level[j,i].SprintReachable)
                        {
                            spriteBatch.Draw(reachbleTexture, new Vector2(i * 50 + 10, j * 50 + 10), Color.Orange * 0.1f);
                        }
                        spriteBatch.Draw(tileTexture, new Vector2(i * 50, j * 50), Color.Green * 0.2f); 
                    }
                }
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
